﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace GameServer
{
    public partial class IpConfig : Form
    {
        public IpConfig()
        {
            InitializeComponent();
            Init();
        }

        void Init()
        {
            lv.GridLines = true;
            lv.FullRowSelect = true;
            lv.View = View.Details;
            lv.Scrollable = true;
            lv.MultiSelect = false; // 不可以多行选择

            lv.Columns.Add("IP列表", 235, HorizontalAlignment.Center);
            //lv.Columns.Add("B", 100, HorizontalAlignment.Left);
            //lv.Columns.Add("C", 100, HorizontalAlignment.Left);   


            this.lv.BeginUpdate();   //数据更新，UI暂时挂起，直到EndUpdate绘制控件，可以有效避免闪烁并大大提高加载速度 
            List<string> lst = ServerDll.GetIpCpnfig();
            for (int i = 0; i < lst.Count; i++)   //添加10行数据 
            {
                ListViewItem lvi = new ListViewItem();
                lvi.ImageIndex = i;     //通过与imageList绑定，显示imageList中第i项图标 
                lvi.Text = lst[i];   
                this.lv.Items.Add(lvi);
            }

            this.lv.EndUpdate();  //结束数据处理，UI界面一次性绘制。 

            this.ck_ipConfig.Checked = !ServerDll.BeIgnoreIp;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lv.SelectedItems.Count > 0)
            {
                ServerDll.DeleteIpConfig(lv.SelectedItems[0].Text);
                lv.SelectedItems[0].Remove();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            IPAddress ip;
            string ipStr = this.tbIp.Text.Trim();
            if (IPAddress.TryParse(ipStr, out ip))
            {
                List<string> lst = ServerDll.GetIpCpnfig();
                if (ServerDll.AddIpConfig(ipStr))
                {
                    ListViewItem lvi = this.lv.Items.Add(ipStr);
                    lvi.Text = ipStr;
                    lst.Add(ipStr);
                    this.tbIp.Text = "";
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("该IP已经在列表中");
                }
            }
            else
            {
                MessageBox.Show("请输入合法的IP地址");
            }
            
        }

        private void ck_ipConfig_CheckedChanged(object sender, EventArgs e)
        {
            if (this.ck_ipConfig.Checked)
            {
                ServerDll.BeIgnoreIp = false;
            }
            else
            {
                ServerDll.BeIgnoreIp = true;
            }
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            Config2 form = new Config2();
            form.ShowDialog();
        }

        private void btnPrecentSetting_Click(object sender, EventArgs e)
        {
            Config2 form = new Config2();
            form.ShowDialog();
        }
    }
}
